package com.tangcco.lockwell.configuration.spring.security;

import cn.hutool.core.date.DateUtil;
import com.tangcco.lockwell.base.SystemCode;
import com.tangcco.lockwell.context.WxContext;
import com.tangcco.lockwell.domain.User;
import com.tangcco.lockwell.domain.UserEventLog;
import com.tangcco.lockwell.domain.UserToken;
import com.tangcco.lockwell.event.UserEvent;
import com.tangcco.lockwell.service.UserEventLogService;
import com.tangcco.lockwell.service.UserService;
import com.tangcco.lockwell.service.UserTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;


/**
 * @version 3.5.0
 * @description: 用户登出
 * Copyright (C), 2020-2024, 乐考无忧
 * @date 2021/12/25 9:45
 */
@Component
public class RestLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {

    private final ApplicationEventPublisher eventPublisher;
    private final UserService userService;
    private final UserTokenService userTokenService;
    private final WxContext wxContext;
    private final UserEventLogService userEventLogService;

    /**
     * Instantiates a new Rest logout success handler.
     *
     * @param eventPublisher the event publisher
     * @param userService    the user service
     */
    @Autowired
    public RestLogoutSuccessHandler(ApplicationEventPublisher eventPublisher, UserService userService, WxContext wxContext, UserTokenService userTokenService, UserEventLogService userEventLogService) {
        this.eventPublisher = eventPublisher;
        this.userService = userService;
        this.wxContext = wxContext;
        this.userTokenService = userTokenService;
        this.userEventLogService = userEventLogService;
    }

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        String token = "";
        String authorization = request.getHeader("Authorization");
        if (authorization!= null && authorization.startsWith("Bearer ")) {
            token = authorization.substring("Bearer ".length());
        }
        UserToken userToken = userTokenService.getToken(token);
        if(userToken!=null && null != userToken.getUserId()){
            User currentUser = userService.getUserById(userToken.getUserId());
            if (null != currentUser) {
                //记录用户登出日志
                UserEventLog userEventLog = new UserEventLog();
                userEventLog.setUserId(currentUser.getUserId());
                userEventLog.setContent("退出了系统");
                userEventLog.setUserId(currentUser.getUserId());
                userEventLog.setUserName(currentUser.getUserName());
                userEventLog.setRealName(currentUser.getRealName());
                userEventLog.setCreateTime(DateUtil.formatDateTime(new Date()));
                userEventLogService.insert(userEventLog);
                //删除token
                userTokenService.deleteUserTokenByUserId(currentUser.getUserId());
            }
        }

        RestUtil.response(response, SystemCode.OK);
    }
}
